package com.sunny.ihealth.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.sunny.ihealth.BaseActivity;
import com.sunny.ihealth.R;
import com.sunny.ihealth.data.BmobManager;
import com.sunny.ihealth.entity.bmob.Post;
import com.sunny.ihealth.entity.bmob.State;
import com.sunny.ihealth.entity.bmob.UserInfo;
import com.sunny.ihealth.ui.adapter.OnViewClickListener;
import com.sunny.ihealth.ui.adapter.ProfileAdapter;
import com.sunny.ihealth.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class ProfileActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, OnViewClickListener {

    private static final String TAG = "TAG::ProfileActivity";

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private ProfileAdapter adapter;
    private Toolbar toolbar;
    private View statusBar;

    private UserInfo userInfo;
    private List<Post> data;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        initViews();
    }


    @Override
    public void initViews() {
        StatusBarUtil.justTransparentStatusBar(this);
        setEnableBackIcon(true);

        if ((userInfo = (UserInfo) getIntent().getSerializableExtra("data")) == null) {
            Toast.makeText(ProfileActivity.this, "没有数据...", Toast.LENGTH_SHORT).show();
            return;
        }
        setToolbar("");

        toolbar = getToolbar();
        statusBar = findViewById(R.id.view_statusBar);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_profile);
        swipeRefreshLayout.setProgressViewOffset(false, ScreenUtils.dp2px(this, 30), ScreenUtils.dp2px(this, 90));
        swipeRefreshLayout.setOnRefreshListener(this);
        adapter = new ProfileAdapter(ProfileActivity.this, userInfo, data = new ArrayList<>());
        adapter.setListener(this);
        linearLayoutManager = new LinearLayoutManager(ProfileActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView_profile);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addOnScrollListener(scrollListener);
        recyclerView.setAdapter(adapter);


        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                initData();
            }
        }, 500);
    }

    @Override
    public void initData() {
        getBmob().getPost(userInfo.getObjectId(), 0, 50, null, new BmobManager.Action1<List<Post>>() {
            @Override
            public void onCompleted(List<Post> posts) {
                if (posts != null) {
                    userInfo.setPostsNumber(posts.size());
                    if (posts.size() == 0) {
                        Toast.makeText(ProfileActivity.this, "啊偶，什么都没加载出来~", Toast.LENGTH_SHORT).show();
                    } else {
                        data.clear();
                        data.addAll(posts);
                        adapter.notifyDataSetChanged();
                    }
                }
                swipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 500);
            }
        });
    }

    @Override
    public void onRefresh() {
        initData();
    }

    @Override
    public void onViewClick(int viewID, final int position) {
        switch (viewID) {
            case R.id.tv_profileHeader_addFriend:
                getBmob().addFriend(userInfo.getObjectId(), new BmobManager.Action1<State>() {
                    @Override
                    public void onCompleted(State state) {
                        if (state.getState() > 0) {
                            adapter.setIsFriend(state.getState());
                        }
                    }
                });
                break;
        }
    }


    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int minAlpha = 63;
            int maxAlpha = 180;
            View header = linearLayoutManager.findViewByPosition(0);
            if (header != null) {
                float offset = Math.abs(header.getTop());
                float height = header.getHeight() / 2;
                int alpha = (int) ((offset / height) * 255F);
                alpha = alpha < minAlpha ? minAlpha : (alpha > maxAlpha ? maxAlpha : alpha);
                statusBar.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
                toolbar.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
                if (alpha == maxAlpha) {
                    toolbar.setTitle(userInfo.getNickname());
                } else {
                    toolbar.setTitle("");
                }
            }
        }
    };
}
